package com.ldl.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ldl.Const;
import com.ldl.entity.User;
import com.ldl.service.UserService;
import com.ldl.service.impl.LdlServiceImpl;
import com.ldl.service.impl.UserServiceImpl;

/**
 * @author Administrator
 * Author : L.D.L
 * E-mail : 1419485293@qq.com
 * Date : 2020年8月4日下午8:34:44
 */
@WebServlet("/login.let")
public class LoginServlet extends HttpServlet{

	private static Logger log1 = LoggerFactory.getLogger(LoginServlet.class) ;
	private UserService userService = new UserServiceImpl();
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String userName = request.getParameter("userName");
		String userPwd = request.getParameter("userPwd");
		String code = request.getParameter("code");
		try{
			User user = userService.login(userName,userPwd);
			Const.codeVerify(request.getSession(), code);
			if (user == null) {
				request.setAttribute("error", "用户名不存在");	
				request.getRequestDispatcher("./msg.jsp").forward(request, response);
			}else if (!user.getUserPwd().equals(userPwd)){
				request.setAttribute("error", "密码错误");	
				request.getRequestDispatcher("./msg.jsp").forward(request, response);
			}else{
				request.getSession().setAttribute(Const.LOGIN_USER_SESSION_NAME, user);
				response.sendRedirect(request.getContextPath()+"/admin/ldl-list.let");
			}
		}catch(Exception ex){
//			ex.printStackTrace();//记调试日志
			log1.debug("{}",ex);
			request.setAttribute("error", ex.getMessage());
			request.getRequestDispatcher("./msg.jsp").forward(request, response);
		}
	}
}
